package com.zh.jbet.core.tournament.scorer;

import com.zh.jbet.core.scorer.GameScorer;
import com.zh.jbet.core.tournament.entity.TeamMatch;
import com.zh.jbet.core.tournament.entity.MatchResult;
import com.zh.jbet.core.tournament.entity.MatchResultBet;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@XmlRootElement(name = "matchScorer")
public class MatchBetScorer implements GameScorer<TeamMatch, MatchResultBet>{
    private int asymptoticPoint;
    private int matchingPoint;

    @Override
    public void reset() {
        asymptoticPoint = 0;
        matchingPoint = 0;
    }

    @XmlAttribute
    public int getAsymptoticPoint() {
        return asymptoticPoint;
    }

    public void setAsymptoticPoint(int asymptoticPoint) {
        this.asymptoticPoint = asymptoticPoint;
    }

    @XmlAttribute
    public int getMatchingPoint() {
        return matchingPoint;
    }

    public void setMatchingPoint(int matchingPoint) {
        this.matchingPoint = matchingPoint;
    }

    @XmlTransient
    @Override
    public Class<MatchResultBet> getBetClass() {
        return MatchResultBet.class;
    }


    @Override
    public int score(TeamMatch match, MatchResultBet bet) {
        int ret = 0;
        boolean ok = false;
        if (match.isFinished() && bet.isFilled()) {
            MatchResult mr = match.getResult();
            if (mr.isAwayWin()) {
                if (bet.isAwayWin()) {
                    ret += asymptoticPoint;
                    ok = true;
                }
            } else if (mr.isHomeWin()) {
                if (bet.isHomeWin()) {
                    ret += asymptoticPoint;
                    ok = true;
                }
            } else {
                if (!bet.isAwayWin() && !bet.isHomeWin()) {
                    ret += asymptoticPoint;
                    ok = true;
                }
            }
            if (ok && mr.getHomeGoalNo().equals(bet.getHomeGoalNo()) && mr.getAwayGoalNo().equals(bet.getAwayGoalNo())) {
                ret += matchingPoint;
            }
        }
        return ret;
    }
}
